<template>
  <div class="login-container">
    <!-- Header section -->
    <div class="header">
      <h1 class="title">智慧农业助手</h1>
      <p class="subtitle">走进智慧农业，创建幸福生活</p>
    </div>
    
    <!-- Login form -->
    <div class="login-card">
      <!-- Login tabs -->
      <div class="login-tabs">
        <div 
          class="tab" 
          :class="{ active: loginType === 'account' }"
          @click="loginType = 'account'"
        >
          <span>账号密码登录</span>
          <div class="tab-indicator" v-if="loginType === 'account'"></div>
        </div>
        <div 
          class="tab" 
          :class="{ active: loginType === 'phone' }"
          @click="loginType = 'phone'"
        >
          <span>手机验证登录</span>
          <div class="tab-indicator" v-if="loginType === 'phone'"></div>
        </div>
      </div>
      
      <!-- Account login form -->
      <div v-if="loginType === 'account'" class="input-group">
        <div class="input-field">
          <div class="icon">
            <i class="user-icon"></i>
          </div>
          <input type="text" placeholder="请输入用户名/手机号" v-model="accountForm.username">
        </div>
        
        <div class="input-field">
          <div class="icon">
            <i class="lock-icon"></i>
          </div>
          <input type="password" placeholder="请输入密码" v-model="accountForm.password">
        </div>
      </div>
      
      <!-- Phone verification login form -->
      <div v-else class="input-group">
        <div class="input-field">
          <div class="icon">
            <i class="phone-icon"></i>
          </div>
          <input type="tel" placeholder="请输入手机号" v-model="phoneForm.phone" maxlength="11">
        </div>
        
        <div class="input-field">
          <div class="icon">
            <i class="code-icon"></i>
          </div>
          <input type="text" placeholder="请输入验证码" v-model="phoneForm.code" maxlength="6">
          <div class="code-button" @click="sendVerificationCode" :class="{ disabled: countdown > 0 }">
            {{ countdown > 0 ? `${countdown}s` : '获取验证码' }}
          </div>
        </div>
      </div>
      
      <!-- Login button -->
      <button class="login-button" @click="login">登 录</button>
    </div>
    
    <!-- Third-party login -->
    <div class="third-party">
      <p class="third-party-title">使用第三方账号登录</p>
      
      <div class="social-icons">
        <div class="social-icon qq">
          <img src="" alt="QQ">
        </div>
        <div class="social-icon wechat">
          <img src="" alt="WeChat">
        </div>
        <div class="social-icon weibo">
          <img src="" alt="Weibo">
        </div>
      </div>
      
      <div class="register">
        <span>没有账号？</span>
        <span class="register-link">手机号快捷登录</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';

// Login type state (account or phone)
const loginType = ref('account');

// Form data
const accountForm = reactive({
  username: '',
  password: ''
});

const phoneForm = reactive({
  phone: '',
  code: ''
});

// Verification code countdown
const countdown = ref(0);
let timer = null;

// Send verification code
const sendVerificationCode = () => {
  // Validate phone number
  if (!phoneForm.phone || phoneForm.phone.length !== 11) {
    alert('请输入正确的手机号码');
    return;
  }
  
  // Start countdown
  countdown.value = 60;
  
  // Clear any existing timer
  if (timer) clearInterval(timer);
  
  // Set up countdown timer
  timer = setInterval(() => {
    countdown.value--;
    if (countdown.value <= 0) {
      clearInterval(timer);
      timer = null;
    }
  }, 1000);
  
  // Here you would typically make an API call to send the verification code
  console.log('Sending verification code to:', phoneForm.phone);
};

// Login function
const login = () => {
  if (loginType.value === 'account') {
    // Validate account login
    if (!accountForm.username) {
      alert('请输入用户名/手机号');
      return;
    }
    if (!accountForm.password) {
      alert('请输入密码');
      return;
    }
    
    console.log('Account login:', accountForm);
  } else {
    // Validate phone login
    if (!phoneForm.phone || phoneForm.phone.length !== 11) {
      alert('请输入正确的手机号码');
      return;
    }
    if (!phoneForm.code) {
      alert('请输入验证码');
      return;
    }
    
    console.log('Phone login:', phoneForm);
  }
  
  // Here you would typically make an API call to authenticate the user
  alert('登录成功');
};
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(180deg, #d6f3ff 0%, #f5f7fa 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 20px;
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica, 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
}

/* Header styles */
.header {
  text-align: center;
  margin-bottom: 40px;
  margin-top: 40px;
}

.title {
  font-size: 28px;
  color: #4db6ac;
  font-weight: bold;
  margin-bottom: 10px;
}

.subtitle {
  font-size: 14px;
  color: #4db6ac;
}

/* Login card styles */
.login-card {
  width: 100%;
  max-width: 320px;
  background-color: #ffffff;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

/* Login tabs */
.login-tabs {
  display: flex;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 20px;
}

.tab {
  flex: 1;
  text-align: center;
  padding: 10px 0;
  font-size: 14px;
  color: #999;
  position: relative;
  cursor: pointer;
}

.tab.active {
  color: #4db6ac;
  font-weight: 500;
}

.tab-indicator {
  position: absolute;
  bottom: -1px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 2px;
  background-color: #4db6ac;
}

/* Input fields */
.input-group {
  margin-bottom: 20px;
}

.input-field {
  display: flex;
  align-items: center;
  border: 1px solid #eaeaea;
  border-radius: 4px;
  margin-bottom: 15px;
  overflow: hidden;
}

.icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  color: #999;
}

.user-icon::before {
  content: "👤";
}

.lock-icon::before {
  content: "🔒";
}

.phone-icon::before {
  content: "📱";
}

.code-icon::before {
  content: "🔑";
}

input {
  flex: 1;
  height: 40px;
  border: none;
  outline: none;
  font-size: 14px;
  padding: 0 10px;
}

/* Verification code button */
.code-button {
  padding: 0 10px;
  height: 30px;
  line-height: 30px;
  background-color: #4db6ac;
  color: white;
  font-size: 12px;
  border-radius: 4px;
  margin-right: 10px;
  cursor: pointer;
  white-space: nowrap;
}

.code-button.disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

/* Login button */
.login-button {
  width: 100%;
  height: 44px;
  background-color: #4db6ac;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
}

/* Third-party login */
.third-party {
  margin-top: 30px;
  text-align: center;
}

.third-party-title {
  font-size: 14px;
  color: #999;
  margin-bottom: 20px;
  position: relative;
}

.third-party-title::before,
.third-party-title::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 60px;
  height: 1px;
  background-color: #eaeaea;
}

.third-party-title::before {
  left: 50%;
  margin-left: -120px;
}

.third-party-title::after {
  right: 50%;
  margin-right: -120px;
}

.social-icons {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 20px;
}

.social-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.social-icon img {
  width: 24px;
  height: 24px;
}

.qq {
  background-color: #12B7F5;
}

.wechat {
  background-color: #07C160;
}

.weibo {
  background-color: #E6162D;
}

.register {
  font-size: 14px;
  color: #999;
  margin-top: 20px;
}

.register-link {
  color: #4db6ac;
  margin-left: 5px;
}
</style>